{{extend '../_layouts/home.html'}}

{{block 'title'}}{{'多人博客 - 首页'}}{{/block}}

{{block 'body'}}
<section class="container">
  <div class="row">
    <div class="col-md-6">
      <form id="new_form">
        <div class="form-group">
          <label for="">选择板块</label>
          <select class="form-control" name="model">
            <option value="0">分享</option>
            <option value="1">问答</option>
            <option value="2">招聘</option>
            <option value="3">客户端测试</option>
          </select>
        </div>
        <div class="form-group">
          <label for="">标题</label>
          <input type="text" class="form-control" name="title">
        </div>
        <div class="form-group">
          <label for="">内容</label>
          <textarea class="form-control" rows="6" name="article"></textarea>
        </div>
        <button type="submit" class="btn btn-success">提交</button>
      </form>
    </div>
  </div>
</section>
{{/block}}
{{block 'script'}}
<script>
  $('#new_form').on('submit',function (e) {
    e.preventDefault()
    if ($('input').val() === '' || $('textarea').val() === ''){
      window.alert('还没有输入标题或内容！')
      return false
    }
    var formData = $(this).serialize()
    $.ajax({
      url: '/topics/new',
      type: 'post',
      data: formData,
      dataType: 'json',
      success: function (data) {
        var err_code = data.err_code
        if (err_code === 0) {
          window.location.href = '/'
        } else if (err_code === 500) {
          window.alert('服务器忙，请稍后重试！')
        }
      }
    })
  })
</script>
{{/block}}
